#! /usr/bin/python
# -*- coding: cp1250 -*-

import sys
import glob
import pepe.p_filesystem.dfstats as dfs
import pepe.p_filesystem.basic as pfb
import pepe.glib as glib
import time

dtnow_iso = lambda : time.strftime ("%Y-%m-%dT%H:%M:%S", time.localtime())
#2008-07-16T19:44:18

sci = glib.sort_case_insensitive

def main(fmask="*.*"):
    ''' calculate checksum for files
    '''
    of = open('chs.#','w')
    
    print dtnow_iso()
    of.write(dtnow_iso() + '\n\n')
    
    #for f in glob.glob(fmask):
    file_list = pfb.fslist(root='.', patterns=fmask, recurse=True, return_folders=False)
    for f in file_list:
        #print file

        rd = dfs.dfstats(f, options=['sha1', 'sha256', 'md5'])
    
        x = "%s\t%s\t*%s\n" % (rd['md5'], 'md5', rd['file_name'])
        #print x; 
        of.write(x)
        x = "%s\t%s\t*%s\n" % (rd['sha1'], 'sha1',rd['file_name'])
        #print x; 
        of.write(x)
        x = "%s\t%s\t*%s\n" % (rd['sha256'], 'sha256',rd['file_name'])
        #print x; 
        of.write(x)
    
    print "pepe.p_utils.chs.py v1.4"
    print dtnow_iso()
    of.write('\n' + dtnow_iso() + '\n')
    of.close()

#main(fmask="2011-03-22_VS-235_smluva_podepsana.pdf")

if __name__ == '__main__':
    ''' '''
    print '''
    Syntax example: chs *.csv;*.txt;*.rtf;*.xlsx
    If run without parameter, *.* mask is used.
    '''
    if  len(sys.argv) == 1:
        main()
    else:
        main(sys.argv[1])
